
function Initialize(){
	// add search type action
	$("#searchtype").change(
		function () {
			var str = $("#searchtype option:selected").val();
                        var platid = $("#searchtype option:selected").attr("platid");
			$("#platid").val( platid );
			//$("#page").val(str);
			$( "#searchform" ).attr("action", str );
		});

	$( "#searchform" ).submit(
		function(){
			CheckSearchAction();
		});

	var str = $('#keyword').val();
	if( "" == str ){
		$("#keyword").val("输入店铺名、掌柜名进行搜索...");
	}
	else{
		$("#keyword").val( str );
	}
}

function CheckSearchAction(){
	if( $("#keyword").val() == "输入店铺名、掌柜名进行搜索..." ){
		$("#keyword").val("");
	}
}

function SetFocus(){
	//	var str = $("#keyword").val();
	//when search input box set to focus, clear the field
	$("#keyword").focus(
		function(){
			if( $("#keyword").val() == "输入店铺名、掌柜名进行搜索..." ){
				$("#keyword").val("");
			}
		}
	)
	$("#keyword").blur(
		function(){
			if( $("#keyword").val() == "" ){
				$("#keyword").val("输入店铺名、掌柜名进行搜索...");
			}
		}
	)
}

function setSelected( id, selectedValue ){
	$('#'+id).val(selectedValue);
}

function setCheckBoxList( id, selectValues ){
	//	alert( selectValues );
	var idArray = selectValues.split(',');
	for(i in idArray){
		$( '#'+id+idArray[i] ).attr('checked', true);
	}
}

function setAllSelectOptions(){
	setSelected( "selcategory", $('#cate_id_list').val() );
	setSelected( "selsortby", $('#sortby').val() );
	setSelected( "sellocation", $('#location').val() );
	setSelected( "selplatfilter", $('#plat_id').val() );
}

function setAllCheckBox(){
	setCheckBoxList( "paymethod", $('#paymethod_list').val() );
	setCheckBoxList( "feature", $('#feature_list').val() );
}

function ProductCatFilter( id ){
	var currentUrl = window.location.toString();
	var result = currentUrl.replace(/&category=[0-9]*/g, "");
	result = result.replace(/#/g, "");
	window.location = result+"&category="+id;
}

function ShowHideCategory( id ){
	if( $( "#catespan_"+id ).is(':hidden') ){
		$( "#catespan_"+id ).show();
		$( "#cate_"+id ).html( "隐藏" );
	}
	else{
		$( "#catespan_"+id ).hide();
		$( "#cate_"+id ).html( "显示更多" );
	}
}

function LoadSortbyClass(){
	var sortby = $('#sortby').val();
	//rate
	if( sortby.indexOf( "rate" ) >= 0 ){
		$( "#sortrate" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortrate" ).addClass( "sortrate" );
	}

	//service
	if( sortby.indexOf( "service" ) >= 0 ){
		$( "#sortservice" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortservice" ).addClass( "sortservice" );
	}

	//reality
	if( sortby.indexOf( "reality" ) >= 0 ){
		$( "#sortreality" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortreality" ).addClass( "sortreality" );
	}

	//velocity
	if( sortby.indexOf( "velocity" ) >= 0 ){
		$( "#sortvelocity" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortvelocity" ).addClass( "sortvelocity" );
	}
}

function SetSortby( type ){
	//reset the sort by selector
	var sortby = $('#sortby').val();
	if( sortby == type+"-asc" ){
		$( "#sortby" ).val( type+"-desc" );
	}
	else if( sortby == type+"-desc" ){
		$( "#sortby" ).val( type+"-asc" );
	}
	else{
		$( "#sortby" ).val( type+"-asc" );
	}
	$( "#filterform" ).submit();
}



function SetCategoryItemClick( cate_id ){
	//set category value and submit the form
	$( "#selcategory" ).val( cate_id );
	$( "#filterform" ).submit();
}

function InitTabClick(){
	if( $("#plat_id").val() >= 0 && $("#plat_id").val() < 100 ){
		$("#c2ctab").addClass( "filter_current" );
	}
	else{
		$("#b2ctab").addClass( "filter_current" );
	}

	$("#c2ctab").click(
		function(){
			//tab css style
			$("#c2ctab").addClass( "filter_current" );
			$("#b2ctab").removeClass( "filter_current" );

			// 0 means c2c
			$("#plat_id").val( 0 );
			$( "#filterform" ).submit();
		}
	);

	$("#b2ctab").click(
		function(){
			//tab css style
			$("#b2ctab").addClass( "filter_current" );
			$("#c2ctab").removeClass( "filter_current" );

			// 100 means b2c
			$("#plat_id").val( 100 );
			$( "#filterform" ).submit();
		}
	);
}
/*
function SetCategoryClick( id ){
	$( "#"+id ).click(
		function(){
			//if is expaned, close it
			if( $( "#"+id+"_span" ).hasClass( "cate_arrow_close" ) ){
				$( "#"+id+"_span" ).removeClass( "cate_arrow_close" );
				$( "#"+id+"_span" ).addClass( "cate_arrow_open" );
				$("#"+id+"_ul").find("li").each(function() {
					$(this).hide();
				});
			}
			else{
				$( "#"+id+"_span" ).removeClass( "cate_arrow_open" );
				$( "#"+id+"_span" ).addClass( "cate_arrow_close" );
				$( "#"+id+"_ul" ).find("li").each(function() {
					$(this).show();
				});
			}

			// must reture false to avoid page redirecting
			return false;
		}
	);
}
*/

function SetCategoryClick( id ){

	
	var str_id = id.replace( "_ul", "" );
	
	$( "#"+str_id ).toggle(function(){
			
		$("#"+id).addClass("expanded"); 

	},function(){
		$("#"+id).removeClass("expanded"); 

	});   
	
	/*
	$( "#"+id ).click(
		function(){
			//if is expaned, close it
			if( $( "#"+id+"_span" ).hasClass( "cate_arrow_close" ) ){
				$( "#"+id+"_span" ).removeClass( "cate_arrow_close" );
				$( "#"+id+"_span" ).addClass( "cate_arrow_open" );
				$("#"+id+"_ul").find("li").each(function() {
					$(this).hide();
				});
			}
			else{
				$( "#"+id+"_span" ).removeClass( "cate_arrow_open" );
				$( "#"+id+"_span" ).addClass( "cate_arrow_close" );
				$( "#"+id+"_ul" ).find("li").each(function() {
					$(this).show();
				});
			}

			// must reture false to avoid page redirecting
			return false;
		}
	);*/
}
	
	
	
$(document).ready(function(){
	Initialize();
	SetFocus();
	setAllSelectOptions();
	setAllCheckBox();
	InitTabClick();

	//sort related block
	LoadSortbyClass();
	$("#sortservice").click(
		function(){
			SetSortby( "service" )
		}
	);
	$("#sortreality").click(
		function(){
			SetSortby( "reality" )
		}
	);
	$("#sortvelocity").click(
		function(){
			SetSortby( "velocity" )
		}
	);
	$("#sortrate").click(
		function(){
			SetSortby( "rate" )
		}
	);

	//select change
	$('#selsortby').change(
		function(){
			var selected = $("#selsortby option:selected");
			//append the sortby and submit the form
			$( "#sortby" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);

	$('#sellocation').change(
		function(){
			var selected = $("#sellocation option:selected");
			//append the localtion and submit the form
			$( "#location" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);

	$('#selplatfilter').change(
		function(){
			var selected = $("#selplatfilter option:selected");
			//append the localtion and submit the form
			$( "#plat_id" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);

	//show hide category logic
	$( "#showAllCategory" ).click(
		function(){
			$("#searchhint_category_less").hide();
			$("#searchhint_category_all").show();
		}
	);

	$( "#showLessCategory" ).click(
		function(){
			$("#searchhint_category_all").hide();
			$("#searchhint_category_less").show();
		}
	);
	

	//category click show/hide
	$( "#allcatelist" ).find("ul").each(
		function() {
			//var str_id = this.id.replace( "_ul", "" );
			//SetCategoryClick( str_id );
			SetCategoryClick( this.id );
		}
	);

	//init all category item click event
	$( "#allcatelist" ).find("a").each(
		function() {
			//big category does not add click event
			if( $(this).parent().get(0).tagName == "LI" ){
				$(this).click(
					function(){
						SetCategoryItemClick( this.id );
						//stop the redirecting
						return false;
					}
				);
			}
		}
	);
});

$(window).load(function() {

	$(".item_list img, .hot_shop img").each(function() {
	
	 if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0 ) || this.readyState == 'uninitialized' ){
					
			this.src = edp_static_domain_url+'image/defaultpicture80.gif';
	}});
	
});
